/*jslint vars: true, forin: true, plusplus: true */
/*global define: false */
define(function (require, exports, module) {
	'use strict';
	var helper = require("model/helper");
	exports.execute = function (me, $, load) {
		var form, options = {};
		return function () {
			helper.moveToCenter(me);
			helper.initWindowTemplate(me);

			$(".add").click(function () {
				form = $("form").serializeObject();
				options[$("[name=key]").val()] = $("[name=pattern]").val();

				load.tpm.refresh(me, {
					options: options,
					form: form
				});
			});

			$(".trash").click(function () {
				form = $("form").serializeObject();
				options = {};
				load.tpm.refresh(me, {
					options: options,
					form: form
				});
			});

			$(".delete").click(function () {
				var key = $(this).parents("[data-key]").attr("data-key");
				form = $("form").serializeObject();
				delete options[key];
				load.tpm.refresh(me, {
					options: options,
					form: form
				});
			});

			$("#create").click(function () {
				var key;
				form = $("form").serializeObject();
				for (key in options) {
					options[key] = new RegExp(options[key]);
				}
				load.dict.createDictionaryByPattern({
					name: form.name,
					keys: options,
					length: form.length,
					comment: form.comment
				});
				options = {};
				form = {};
				load.tpm.close(me);
			});

		};
	};
});